package com.cxyd.modules.wms.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cxyd.common.page.PageData;
import com.cxyd.common.service.impl.CrudServiceImpl;
import com.cxyd.common.utils.ConvertUtils;
import com.cxyd.modules.sys.dao.SysUserDao;
import com.cxyd.modules.sys.entity.SysUserEntity;
import com.cxyd.modules.sys.service.SysUserService;
import com.cxyd.modules.wms.dao.LogWorkDao;
import com.cxyd.modules.wms.dto.LogWorkDTO;
import com.cxyd.modules.wms.entity.LogWorkEntity;
import com.cxyd.modules.wms.service.LogWorkService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

/**
 * 容器信息
 *
 * @author g_Cycle gengruyi01@163.com
 * @since 1.0.0 2023-04-27
 */
@Service
public class LogWorkServiceImpl extends CrudServiceImpl<LogWorkDao, LogWorkEntity, LogWorkDTO> implements LogWorkService {
    private final SysUserDao sysUserDao;
    private final LogWorkDao logWorkDao;

    public LogWorkServiceImpl(SysUserService userService, SysUserDao sysUserDao, LogWorkDao logWorkDao) {
        this.sysUserDao = sysUserDao;
        this.logWorkDao = logWorkDao;
    }

    @Override
    public PageData<LogWorkDTO> page(Map<String, Object> params) {
        //1.查询日志信息
        final IPage<LogWorkEntity> page = baseDao.selectPage(
                getPage(params, "id", false),
                getWrapper(params)
        );
        return getPageData(page, LogWorkDTO.class);
    }

    @Override
    public QueryWrapper<LogWorkEntity> getWrapper(Map<String, Object> params) {
        String workName = (String) params.get("workName");
        String relateNo = (String) params.get("relateNo");
        String workProc = (String) params.get("workProc");
        String workMsg = (String) params.get("workMsg");
        String startDate = (String) params.get("startDate");
        String endDate = (String) params.get("endDate");

        QueryWrapper<LogWorkEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(workName), "work_name", workName);
        wrapper.eq(StringUtils.isNotBlank(relateNo), "relate_no", relateNo);
        wrapper.like(StringUtils.isNotBlank(workProc), "work_proc", workProc);
        wrapper.like(StringUtils.isNotBlank(workMsg), "work_msg", workMsg);
        wrapper.ge(StringUtils.isNotBlank(startDate), "create_date", StringUtils.isNotBlank(startDate) ? DateUtil.beginOfSecond(DateUtil.parse(startDate)) : startDate);
        wrapper.le(StringUtils.isNotBlank(endDate), "create_date", StringUtils.isNotBlank(endDate) ? DateUtil.beginOfSecond(DateUtil.parse(endDate)) : endDate);
        return wrapper;
    }

    @Override
    public void logInsertByJob(LogWorkDTO dto) {
        final LogWorkEntity entity = ConvertUtils.sourceToTarget(dto, LogWorkEntity.class);
        baseDao.insert(entity);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void logWorkInsert(LogWorkEntity logWorkEntity) {
        SysUserEntity userEntity = sysUserDao.selectById(logWorkEntity.getCreator());
//        String userName;
//        if (ObjectUtils.isEmpty(userEntity)) {
//            userName = "未知用户";
//        } else {
//            userName = userEntity.getRealName();
//        }
        String workMsg = logWorkEntity.getWorkMsg();
        if (StringUtils.isNotBlank(workMsg) && workMsg.length() > 450) {
            logWorkEntity.setWorkMsg(workMsg.substring(0, 400));
        }
        //计算天数差
        long l = new Date().getTime() - logWorkEntity.getCreateDate().getTime();
        logWorkEntity.setWorkSec(String.valueOf(l));
        logWorkEntity.setCreatorName(userEntity.getUsername());
        logWorkEntity.setCreateDate(new Date());
        logWorkDao.insert(logWorkEntity);
    }
}